package com.seecen.service.impl;

import com.seecen.bo.RelationBo;
import com.seecen.bo.UserBo;
import com.seecen.cons.Cons;
import com.seecen.dao.RelationDao;
import com.seecen.dao.UserDao;
import com.seecen.exception.AccountLockedException;
import com.seecen.exception.PasswordNotMatcherException;
import com.seecen.exception.UnknownUserException;
import com.seecen.pojo.Relation;
import com.seecen.pojo.UserInfo;
import com.seecen.pojo.UserMain;
import com.seecen.service.UserService;
import com.seecen.utils.IdGenerator;
import com.seecen.vo.IndexVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RelationDao relationDao;

    @Override
    public boolean save(UserMain userMain, UserInfo userInfo) {
        //判断用户名是否存在
        if (userDao.exists(userMain.getUsername()) > 0) {
            return false;
        }
        //保存数据
        IdGenerator ig = new IdGenerator();
        int id = ig.snowflakeId().intValue();
        userMain.setId(id);
        userInfo.setId(id);
        userDao.saveMain(userMain);
        userDao.saveInfo(userInfo);

        return true;
    }

    @Override
    public UserMain login(UserMain userMain) {
        UserMain dbUserMain = userDao.queryUser(userMain.getUsername());
        if (dbUserMain == null) {
            throw new UnknownUserException();
        }
        if (!dbUserMain.getPassword().equals(userMain.getPassword())) {
            throw new PasswordNotMatcherException();
        }
        if (dbUserMain.getValid() == 0) {
            throw new AccountLockedException();
        }
        return dbUserMain;
    }

    @Transactional(readOnly = true)
    public IndexVo findIndexVo(Integer id) {
        IndexVo indexVo = new IndexVo();
        List<RelationBo> list = relationDao.findMyUnReadRelationCount(id);
        for (RelationBo bo : list) {
            if (bo.getType() == Cons.RELATION_LOOK) {
                indexVo.setLookMeCount(bo.getCount());
            }
            if (bo.getType() == Cons.RELATION_MESSAGE) {
                indexVo.setUnreadCount(bo.getCount());
            }
            if (bo.getType() == Cons.RELATION_FOLLOW) {
                indexVo.setFollowCount(bo.getCount());
            }
        }
        // 先通过ID查询自己的INFO信息，里面有生日，性别
        UserInfo userInfo = userDao.findInfoById(id);

        UserBo userBo = new UserBo();

        userBo.setGender(userInfo.getGender() == 0 ? 1 : 0);

        Calendar calendar = Calendar.getInstance();

        calendar.setTime(userInfo.getBirthday());

        calendar.add(Calendar.YEAR,-4);

        userBo.setStart(calendar.getTime());

        calendar.add(Calendar.YEAR,8);

        userBo.setEnd(calendar.getTime());

        int count = userDao.findUserCount(userBo);

        userBo.setStartIndex((int) (Math.random() * (count - 4)));

        indexVo.setUsers(userDao.findRecommendFriend(userBo));

        return indexVo;
    }

    @Transactional
    public void updateFace(Integer id, String facepath) {
        userDao.updateFace(id, facepath);
    }
}
